import { $t } from '@/locales';
import { NEllipsis } from 'naive-ui';
import { h } from 'vue';

/**
 * Transform record to option
 *
 * @example
 *   ```ts
 *   const record = {
 *     key1: 'label1',
 *     key2: 'label2'
 *   };
 *   const options = transformRecordToOption(record);
 *   // [
 *   //   { value: 'key1', label: 'label1' },
 *   //   { value: 'key2', label: 'label2' }
 *   // ]
 *   ```;
 *
 * @param record
 */
export function transformRecordToOption<T extends Record<string, string>>(record: T) {
    return Object.entries(record).map(([value, label]) => ({
        value,
        label
    })) as CommonType.Option<keyof T>[];
}

/**
 * Translate options
 *
 * @param options
 */
export function translateOptions(options: CommonType.Option<string>[]) {
    return options.map((option) => ({
        ...option,
        label: $t(option.label as App.I18n.I18nKey)
    }));
}

/**
 * Toggle html class
 *
 * @param className
 */
export function toggleHtmlClass(className: string) {
    function add() {
        document.documentElement.classList.add(className);
    }

    function remove() {
        document.documentElement.classList.remove(className);
    }

    return {
        add,
        remove
    };
}

/**
 * check null
 *
 * @param obj
 */
export function isNull(obj: any) {
    return obj === null || obj === undefined || obj === '';
}

/**
 * check not null
 *
 * @param obj
 */
export function isNotNull(obj: any) {
    return !isNull(obj);
}

/** 快速返回 formatDateColumn */
export function renderDateColumn(time: number | null, format: string = 'YYYY-MM-DD HH:mm:ss') {
    return h('div', null, {
        default: () => (isNull(time) ? '-' : formatTimestamp(time as number, format))
    });
}

/** 快速返回 formatNumberColumn */
export function renderNumberColumn(number: number | null, props?: any) {
    return h('div', props, {
        default: () => (isNull(number) ? '/' : formatNumber(number as number))
    });
}

/** 快速返回 formatDecimalColumn */
export function renderDecimalColumn(number: number | null, scale: number = 4, props?: any) {
    return h('div', props, {
        default: () => (isNull(number) ? '/' : transformDecimal(number as number, scale))
    });
}

/** 快速返回 formatFileSizeColumn */
export function renderFileSizeColumn(number: number | null, props?: any) {
    return h('div', props, {
        default: () => (isNull(number) ? '/' : formatFileSize(number as number))
    });
}

/** 渲染 Div Column */
export function renderDivColumn(text: string | null | undefined, props?: any) {
    const txt = isNotNull(text) ? text : '-';
    return h('div', props, { default: () => txt });
}

/** 渲染 Ellipsis Column */
export function renderEllipsisColumn(
    text: string | null,
    style: Record<string, string> = { maxWidth: '300px' }
) {
    const txt = isNotNull(text) ? text : '-';
    return h(NEllipsis, { style }, { default: () => txt });
}

/**
 * 覆盖用户习惯列表头部
 *
 * @param tableKey tableKey
 * @param columns 待覆盖字段
 */
export async function doOverrideHabitsColumns(tableKey: string, columns: any[]) {
    // const columnMap = await getUserTableHabits(tableKey);
    const columnMap = undefined;
    // 覆盖属性
    if (columnMap !== undefined) {
        // 追加 order
        // let order = 0;
        // columnMap.values().forEach((row) => {
        //   row.order = order;
        //   order += 1;
        // });
        // // 覆盖 order
        // columns.forEach((column) => {
        //   column.minWidth = 150;
        //   column.resizable = true;
        //   let habitColumn = columnMap.get(column.key);
        //   if (!habitColumn) {
        //     habitColumn = columnMap.get(column.type);
        //   }
        //   if (!habitColumn) {
        //     column.visible = false;
        //   } else {
        //     if (habitColumn.width) {
        //       column.width = habitColumn.width;
        //     }
        //     column.order = habitColumn.order;
        //     column.visible = habitColumn.visible;
        //   }
        // });
    }
    // 默认全部可见
    else {
        columns.forEach((column) => (column.visible = true));
    }

    columns.sort((a, b) => a.order - b.order);

    return columns;
}
